package io.gitee.yth.design.pattern.test.core.pattern.behavioral.interpreter;

/**
 * @author yth
 * @since 2023-05-01-22:48
 */
public class VariableExp implements BooleanExp {

    private final String name;

    public VariableExp(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean evaluate(Context context) {
        return context.lookup(name);
    }

    @Override
    public BooleanExp replace(String var, BooleanExp exp) {
        return name.equals(var)
                ? exp.copy()
                : this;    // 享元：负责细粒度对象重复创建
    }

    @Override
    public BooleanExp copy() {
        return this;    // 享元：负责细粒度对象重复创建
    }

}
